package com.huazai.yangge.cas;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.StampedLock;

/**
 * @author huazai
 * @date 2024/2/27
 */
public class lockDownGradingDemo {
    public static void main(String[] args) {
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

        // 正常两个线程A B
        //writeLock.lock();
        //System.out.println("----------写入");
        //writeLock.unlock();
        //
        //readLock.lock();
        //System.out.println("----------读取");
        //readLock.unlock();

        // 写锁降级为读锁
        //writeLock.lock();
        //System.out.println("----------写入");
        //
        //readLock.lock();
        //System.out.println("----------读取");
        //readLock.unlock();
        //
        //writeLock.unlock();

        // 锁不可以升级的  及读锁没有释放写锁不能进入
        readLock.lock();
        System.out.println("----------读取");

        writeLock.lock();
        System.out.println("----------写入");
        writeLock.unlock();

        readLock.unlock();


        // 比读写锁更快的锁 邮戳锁：乐观读锁， 其他线程尝试获取写锁时不会阻塞，这就是对读锁的优化
        // 3中访问模式：
        //     reading乐观度
        //     read
        //     optimistic
        StampedLock stampedLock = new StampedLock();



    }
}
